var express = require('express');
var router = express.Router();
var script = require('./lib/script');
var https = require('https');

router.get('/formatProduct/:pids', function(req, res) {
    var pids = req.params.pids || false;
    if(pids === 'geek'){
        pids = false;
    }else if(pids === 'all'){
        pids = true;
    }else{
        pids = pids.split(',');
    }
    script.formatProduct(pids).then(function(ss){
        res.send('ok');
    }).catch(function(e){
        res.send(e);
    });

});

router.get('/formatComment',function(req,res){
    script.formatComment().then(function(ss){
        res.send('ok');
    }).catch(function(e){
        res.send(e);
    });
});

router.get('/closeOrder',function(req,res){
    script.closeOrder().then(function(ss){
        res.send('ok');
    }).catch(function(e){
        res.send(e);
    });
});

router.get('/clean', function(req, res) {
    script.cleanOrder().then(function(c){
        res.json(c);
    }).catch(function(e){
        res.json(e);
    });

});

var options = {
    hostname: 'leancloud.cn',
    port: 443,
    path: '/1.1/push',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        //'X-LC-Id':'64yipwcn159l9ehdf3j3x3xz1f1ly9r89x9gf7i2d1282dvz',
        //'X-LC-Key':'w3xp0oibbzhweamm76ogo6r5dyzt82tczw7lssfvyj6hlarv'
    },
    //body:'{"channels":[ "public"],"data": {"alert": "LeanCloud 向您问好！"}}'
};

router.get('/push', function(req, rsp) {
    console.log('aa');
    https.request({
        hostname: 'leancloud.cn',
        port: 443,
        path: '/1.1/push',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            //'X-LC-Id':'64yipwcn159l9ehdf3j3x3xz1f1ly9r89x9gf7i2d1282dvz',
            //'X-LC-Key':'w3xp0oibbzhweamm76ogo6r5dyzt82tczw7lssfvyj6hlarv'
        },
        //body:'{"channels":[ "public"],"data": {"alert": "LeanCloud 向您问好！"}}'
    },function (res) {
        console.log('STATUS: ' + res.statusCode);
        console.log('HEADERS: ' + JSON.stringify(res.headers));
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            console.log('BODY: ' + chunk);
        });
        rsp.send('ok');
    });


});

module.exports = router;

